<%@page import="org.proddevplm.dao.data.prioritize.AHPAnalysis"%>
<%@page import="org.proddevplm.dao.data.activities.Activity"%>
<%@page import="java.util.List"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="org.proddevplm.constants.ControllerConstants"%>
<%@page import="org.proddevplm.business.activity.constants.ActivityConstants" %>
<%@page import="org.proddevplm.business.tools.constants.ToolsConstants" %>

<%
List<Activity> activitiesForUser = (List)session.getAttribute("activitiesForUser");
List<AHPAnalysis> ahpByActName = (List)session.getAttribute("ahpByActName");
%>

<script>
	var ahpRes = new Array();
	var dataType = "";
	
	$(function(){
		$( "button" ).button();
		$( ".progressbar" ).progressbar();
		$("#leftPane").hide();
		$("#rightPane").hide();
		$("#ahpAproverTable").tablesorter({
			headers:{
				1:{sorter: false}
			},
			sortList: [[2,1]]
		})
	});

	
	function loadAHPPage(page){
		$("#getElements").load(page);
	}
	
	function loadAHPPageWithReq(request, page) {
        $.post(request, function(data) {
            $("#getElements").load(page);
        });
	}
	
	function loadAHPWorkspace(page){
		$("#generateWorkspace").load(page);
	}
	
	function getSH(){
		loadAHPPageWithReq("/proddev/controller?manager=dataAccessManager&method=4","content/project/devTools/workspace/prioritization/getStakeholders.jsp");
	}
	
	function getRequirements(){
		loadAHPPageWithReq("/proddev/controller?manager=dataAccessManager&method=9","content/project/devTools/workspace/prioritization/getRequirements.jsp");
	}
	
	function addElements(){
		loadAHPPage("content/project/devTools/workspace/prioritization/getOtherElements.jsp");
	}
	
	function showProgressbar(index, val){
			
		var value = val*100;
		var id = "td_"+index;
		$('#'+id).progressbar("option", "value", value);
	}
	
	<%if((ahpByActName!=null) && (ahpByActName.size()!=0)){
		for(int l=0; l<ahpByActName.size(); l++){
			AHPAnalysis ahp = ahpByActName.get(l);
			String imp = ahp.getImportances();
			String[] aux = imp.split("~");
			Float[] ahpResults = new Float[aux.length];
			for (int j=0; j<aux.length; j++){
				ahpResults[j] = Float.valueOf(aux[j]);
				%>ahpRes[<%=j%>]=<%=ahpResults[j]%>;<%
			}%>
			for(var i=0; i<ahpRes.length; i++){
				showProgressbar(i, ahpRes[i]);
			}
		<%}
	}%>
	
	function approveActivity(name){
		$("#activityName").val(name);
		document.forms['approveForm'].submit();
	}
	
	function rejectActivity(name){
		$("#activityName").val(name);
		$("#rejectDialog").dialog("open");
	}
	
	$( "#rejectDialog" ).dialog({
		autoOpen: false,
		height: 350,
		width: 350,
		modal: true,
		buttons: {
			"Propose change": function() {
				var name = document.getElementById("activityName").value;
				var arguments = "activityName="+name+"&"+buildParameter("changeSubject")+"&"+buildParameter("changeDetails");
				ajaxCall("toolsManager", "21", arguments, forward);
				$( this ).dialog( "close" );
				},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "close" );
		}
	});
	
	function forward(data){
		document.forms["rejectForm"].submit();
	}
	
</script>

<div class="pageTitle">
	<div style="text-align:left;">
		<a href="/proddev/controller?manager=dataAccessManager&method=21">
			<img src="/proddev/images/arrowleft.png" align="middle" height="20" width="30" style="border:0px"></a>
	</div>
	<p style="font-size:small; font-weight:bold; margin-top:-20px">Analytical Hierarchy Process Technique</p>

</div>

<%if(ahpByActName==null){ %>
<div style="margin:10 0 0;">
	<div style="float:left; text-align:left; font-weight:bold; color:#555555">
		Please choose the activity you want to complete
	</div>
	<select class="regInput" id="activitySelect" name="activitySelect" onchange="$('#leftPane').show(), $('#rightPane').show()" size="1" style="width:25%; float:left; margin-left:10px">
		<option SELECTED>-select activity-</option>
		<%if(activitiesForUser!=null){
			for(int i=0; i<activitiesForUser.size(); i++) {
				Activity activity = (Activity)activitiesForUser.get(i);%>
				<option value="<%=activity.getName()%>"><%=activity.getName() %></option>
			<%}
		}
		%>		
	</select>
	<div style="clear:both"></div>
</div>

<div id="leftPane" style="width:25%; float:left; ">
	<button id="getSH" name="getSH" onclick="getSH()" style="width:80%; margin-top:5px; font-weight: bold; font-size:smaller; float:center;">Get stakeholders</button>
	<button id="getRequirements" name="getRequirements" onclick="getRequirements()" style="width:80%; margin-top:5px; font-weight: bold; font-size:smaller; float:center;">Get requirements</button>
	<button id="addElements" name="addElements" onclick="addElements()" style="width:80%; margin-top:5px; font-weight: bold; font-size:smaller; float:center;">Add elements</button>
	
	
	<div style="margin-top:10px; width:100%">
		<jsp:include page="/pages/content/project/devTools/workspace/prioritization/getElements.jsp"></jsp:include>
	</div>
	
</div>

<div id="rightPane" style="width:75%; float:left;">
	<jsp:include page="/pages/content/project/devTools/workspace/prioritization/workspace.jsp"></jsp:include>
	<div style="clear:both"></div>
</div>
<div style="clear:both"></div>
<%}else{%>
	<%for(int i=0; i<ahpByActName.size(); i++){
		AHPAnalysis a = ahpByActName.get(i); 
		String elements = a.getElements();
		String[] elArray = elements.split("~");
		String importances = a.getImportances();
		String[] aux = importances.split("~");
		Float[] iArray = new Float[elArray.length];
		for (int k=0; k<aux.length; k++){
			iArray[k] = Float.valueOf(aux[k]);
		}%>
		
		<div class="containerSmall" style="margin-top:10px; width:auto;">
			<div style="width:100%; margin-top:10px">
				<div style="width:30%; text-align:left; float:left">
					<b>Analysis name:</b>
				</div>
				<div style="width:60%; text-align:left; float:left">
					<%=a.getName() %>
				</div>
				<div style="clear:both"></div>
			</div>
				
			<div style="width:100%; margin-top:10px">
				<div style="width: 30%; text-align:left; float:left;">
					<b>Analysis author:</b>
				</div>
				<div style="width:60%; text-align:left; float:left">
					<%=a.getAuthor() %>
				</div>
				<div style="clear:both"></div>
			</div>
			<table class="tablesorter" id="ahpAproverTable" rules="rows" frame="hsides" bordercolor="#DBDBDB" style="font-size:small; margin-top:10px; width:100%;">
				<thead>
					<tr>
						<th>Element</th>
						<th>Importance</th>
						<th>&nbsp</th>
					</tr>
				</thead>
					
				<%for(int j=0; j<elArray.length; j++){			
					Float value = iArray[j]*100;
					DecimalFormat dFormat = new DecimalFormat("0.00");
					String shownVal = dFormat.format(value)+"%";
				%>
				<tr>
					<td ><%=elArray[j] %></td>
					<td class="progressbar" id="td_<%=j %>" style="width:auto; font-size:xx-small; border:0px" ></td>
					<td style="width:50px; font-weight:bold; background-color:#DBDBDB;"><%=shownVal%></td>
				</tr>
				
			<%}%>
			</table>
			
			<%}%>
			<%
			String activityName = "";
			AHPAnalysis a = ahpByActName.get(0);
			activityName = a.getActivityName();
			%>
			<div style="width:100%">
				<button id="rejectActivity" name="rejectActivity" onclick="rejectActivity('<%=activityName%>')" style="width:150px; font-weight: bold; float:left">Reject activity</button>
				<button id="approveActivity" name="approveActivity" onclick="approveActivity('<%=activityName%>')" style="width:150px; font-weight: bold; float:right;">Approve activity</button>
				<div style="clear:both"></div>
			</div>
			<div style="clear:both"></div>
		</div>
		
		<div id="rejectDialog" title="Propose changes" style="display:none">
			<jsp:include page="/pages/content/project/devTools/workspace/changes/changeReport.jsp"></jsp:include>
		</div>
	
		<form name="rejectForm" method="post" action="/proddev/controller" style="margin:0px">
			<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.TOOLS_MANAGER%>"/>
			<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ToolsConstants.STAKEHOLDERS_FWD%>"/>
		</form>
				
		<form name="approveForm" method="post" action="/proddev/controller" style="margin:0px">
			<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.ACTIVITY_MANAGER%>"/>
			<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ActivityConstants.UPDATE_ACTIVITY%>"/>
			<input type="hidden" name="activityName" id="activityName">
		</form>
<%}%>